package com.example.note_project.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import android.widget.Toast;
import androidx.annotation.RequiresApi;

import com.example.note_project.util.PicturePickUtils;
import com.example.note_project.R;
import com.example.note_project.util.UriUtils;
import com.example.note_project.ui.tools.PictureAndTextEditorView;

import java.util.ArrayList;
import java.util.List;

public class EditActivity extends Activity{

  private PictureAndTextEditorView mEditText;
  private Button mButton;
private ImageView img;
private ImageView img1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_detail);

img=(ImageView)findViewById(R.id.iv_noteactivity_back);
img1=(ImageView)findViewById(R.id.button_add_picture);
img.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
      AlertDialog.Builder builder  = new AlertDialog.Builder(EditActivity.this);
      builder.setTitle("确认" ) ;
      builder.setMessage("是否保存？" ) ;
      builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              Toast.makeText(EditActivity.this, "笔记已保存", Toast.LENGTH_SHORT).show();
              Intent intent = new Intent(EditActivity.this, MainActivity.class);
              startActivity(intent);
          }
      });
      //设置监听器
      builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              Toast.makeText(EditActivity.this, "笔记未保存", Toast.LENGTH_SHORT).show();
              Intent intent = new Intent(EditActivity.this, MainActivity.class);
              startActivity(intent);
          }
      });
      builder.show();
  }
});
    mEditText = (PictureAndTextEditorView) findViewById(R.id.it_mixture);
    //mButton = (Button) findViewById(R.id.button_add_picture);
    ///storage/emulated/0/Mob/cn.xfz.app/cache/images/1450915925_4457.jpg
    ///storage/emulated/0/Pictures/1450770237621.jpg
    ///storage/emulated/0/Pictures/1450769835187.jpg
    ///storage/emulated/0/Mob/cn.xfz.app/cache/images/1450684805_82970.jpg
    List<String> list = new ArrayList<>();//这里是测试用的，对于图片地址，各位还是要自己设置一下
    list.add("你说");
    list.add(PictureAndTextEditorView.mBitmapTag+"/storage/emulated/0/Pictures/1450915925_4457.jpg");
    list.add("我在哪");
    list.add(PictureAndTextEditorView.mBitmapTag+"/storage/emulated/0/Pictures/1450770237621.jpg");
    list.add("不告诉你");
    list.add(PictureAndTextEditorView.mBitmapTag+"/storage/emulated/0/Pictures/1450769835187.jpg");
    list.add("嘿嘿");
    list.add(PictureAndTextEditorView.mBitmapTag+"/storage/emulated/0/Pictures/1450684805_82970.jpg");

//        mEditText.setmContentList(list);


    img1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        PicturePickUtils.selectPicFromLocal(EditActivity.this,888);//获取手机本地图片的代码，大家可以自行实现
     }
   });

    mEditText.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {

      }

      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.i("EditActivity",mEditText.getmContentList().toString());
      }

      @Override
      public void afterTextChanged(Editable s) {

      }
    });
  }

  @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case 888:
          if (data != null) {

            Uri selectedImage = data.getData();
            String imageurl = UriUtils.getPath(this, selectedImage);

            mEditText.insertBitmap(imageurl);



          }
        default:
          break;
      }
    }
  }
}
